'use strict';
const path = require('path');

const Controller = require('egg').Controller;

// 定义创建接口的请求参数规则
// const createRule = {
//   file: {
//     required: true,
//   },
// };

const getFileType = mimetype => {
  switch (mimetype) {
    case 'image/gif':
    case 'image/png':
    case 'image/jpeg':
    case 'image/bmp':
    case 'image/webp':
      return 0;
    case 'audio/midi':
    case 'audio/mpeg':
    case 'audio/webm':
    case 'audio/ogg':
    case 'audio/wav':
      return 1;
    case 'video/webm':
    case 'video/ogg':
    case 'video/mp4':
      return 2;
    default:
      return 3;
  }
};

class ResourceController extends Controller {
  async index() {
    const ctx = this.ctx;
    const { cms: { material } } = ctx.service;
    const data = await material.find(ctx.query);
    ctx.success({ data });
  }

  async show() {
    const ctx = this.ctx;
    const { cms: { material } } = ctx.service;
    const data = await material.findOne(ctx.params);
    ctx.success({ data });
  }

  async create() {
    const ctx = this.ctx;
    const { cms: { material } } = ctx.service;
    // ctx.validate(createRule);
    try {
      const parts = ctx.multipart();
      const params = {};
      let object;
      let part;
      let filename;
      let type;
      while ((part = await parts())) {
        if (part.length) {
          // arrays are busboy fields
          // field: part[0]; value: part[1]; valueTruncated: part[2]; fieldnameTruncated: part[3];
          const field = part[0];
          const value = part[1];
          params[field] = value;
        } else {
          // otherwise, it's a stream
          // field: part.fieldname; filename: part.filename; encoding: part.encoding; mime: part.mime;
          // file handle
          const parseFile = path.parse(part.filename);
          filename = part.filename;
          type = getFileType(part.mime);
          object = await ctx.oss.put(`egg-example-${parseFile.name}-${new Date().getTime()}${parseFile.ext}`, part);
        }
      }
      if (object) {
        const ext = path.parse(filename).ext;
        const name = params.name ? path.parse(params.name).name + ext : filename;
        // const type = 0;
        const url = object.url;
        const description = params.description;
        const id = await material.create({
          name, type, url, description,
        });
        ctx.success({ data: { id, url: object.url } });
      } else {
        ctx.error({ detail: 'please select a file to upload！' });
      }
    } catch (error) {
      console.log(error);
      ctx.error({ detail: error });
    }
  }

  async destroy() {
    const ctx = this.ctx;
    const { cms: { material } } = ctx.service;
    try {
      const object = await ctx.oss.delete('egg-oss-demo-1529918714926.txt');
      const data = await material.destroy(ctx.params);
      console.log(object);
      ctx.success({ data });
    } catch (error) {
      ctx.error({ detail: error });
    }
  }
}

module.exports = ResourceController;
